package test;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParseHtml {

    public static void main(String[] args) throws IOException {
               String url = "http://php.szghrj.com:7878/";
               Document doc = Jsoup.connect(url).get();

               //出奖日期
               Element elementKaijiangDate =  doc.getElementsByClass("my-6").first();
               Elements elementsDaojishiDiv = elementKaijiangDate.getElementsByTag("div").first().children();

               Iterator<Element>  it = elementsDaojishiDiv.iterator();
               int i = 0 ;
               int secondes = 0;
               while (it.hasNext()){
                   Element e = it.next().child(0);
                   int     n = Integer.parseInt(e.html());
                   switch (i)
                   {
                       case 0:
                           secondes += 24*n*3600;
                           break;
                       case 1:
                           secondes += n*3600;
                           break;
                       case 2:
                           secondes += n*60;
                           break;
                       case 3:
                           secondes += n;
                           break;

                   }
                   i++;
               }

             TimeZone timeZone = TimeZone.getTimeZone("GMT+07:00");
             Calendar c = Calendar.getInstance(timeZone);
             c.add(Calendar.SECOND,secondes);
             int year = c.get(Calendar.YEAR);
             int month = c.get(Calendar.MONTH)+1;
             int day   = c.get(Calendar.DAY_OF_MONTH);
             int hour = c.get(Calendar.HOUR_OF_DAY);
             int minute = c.get(Calendar.MINUTE);
             int second = c.get(Calendar.SECOND);
             String chuJiangShijian = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second; //有误差
             System.out.println("本期开奖时间:"+chuJiangShijian);

             //上一期开奖时间和结果
             Element elementSanQiKaijiangDate =  doc.getElementsByClass("my-6").last();
             String sanQiRiqi = elementSanQiKaijiangDate.child(0).html();
                Matcher matcher = Pattern.compile("[0-9]{2}/[0-9]{2}/[0-9]{4}").matcher(sanQiRiqi);
                if (matcher.find()){
                    String[] dateArray = matcher.group().split("/");
                    String dateStr = dateArray[2]+"-"+dateArray[1]+"-"+dateArray[0];
                    System.out.println("上期开奖日期:"+dateStr);
                }

             //上期开奖结果
            Elements elementsKaijiangJieGuoSvg =  elementSanQiKaijiangDate.child(1).children();
            StringBuffer  sb = new StringBuffer();
            elementsKaijiangJieGuoSvg.iterator().forEachRemaining(e -> {
                 String numStr = e.getElementsByTag("text").first().html();
                 sb.append(numStr);
            });
           System.out.println("上期开奖号码:"+sb);


           }





}
